﻿/**
 * ...
 * @author krozalski
 */

package hapi.util.profiler;
import haxe.PosInfos;
import haxe.Timer;

class Profiler {
	
	static var currentBlock:CodeBlock = new CodeBlock("");
	static var globalBlock:CodeBlock = currentBlock;
	static var memoryUsage:CodeMemoryUsage = new CodeMemoryUsage();
	
	public static function leaveAndEnterAnother(blockToEnterName) {
		leaveBlock();
		enterBlock(blockToEnterName);
	}
		
	public static function enterBlock(blockName) {
		currentBlock = currentBlock.enter(blockName);
	}
	
	public static function leaveBlock() {
		currentBlock = currentBlock.leave();
	}
	
	public static function markMemoryUsage(label, ?posInfos:PosInfos) {
		memoryUsage.mark(label, posInfos);
	}
	
	public static function summary() {
		var buf = new StringBuf();
		buf.add("Execution Time: ");
		addSystemCapabilities(buf);
		buf.add(globalBlock.summary());
		buf.add("\n----------------------------------------------\n");
		buf.add("Memory Usage: \n");
		buf.add(memoryUsage.summary());
		return buf.toString();
	}
	
	static function addSystemCapabilities(buf:StringBuf) {
		#if flash9
		var cap = flash.system.Capabilities;
		var isDebugger = cap.isDebugger ? "DEBUGGER" : "";
		buf.add("( " + cap.os + " " + cap.playerType + " " + isDebugger + " " + cap.version+" ) ");
		#end
	}
}